package com.blocks.blocks;

import com.blocks.script.ScriptUtils;
import com.jme3.math.Quaternion;

public class RotateBlock extends Block{
	public float angle = 0.0f;
	public int axis = 0;
	public RotateBlock() {	
		super();
	}
	public RotateBlock(String name, Long masterId) {
		super(name, masterId);
		
	}
	@Override
	public void init() {
		setTextureRow(0.0f);
		setTextureColumn(3.0f);
		 super.init();
		 
	}
	
	@Override
	public void updateLogicalState(float tpf) {
		super.updateLogicalState(tpf);
		if(isPowered()){
	     int freqValue = getFrequency(getBlockSides())!=0 ? getFrequency(getBlockSides()) : 1;
		 this.angle = angle +(tpf*freqValue);
		 float yaw = 0.0f;
		 float roll = 0.0f;
		 float pitch = 0.0f;
		 if(axis==0){
			 yaw = angle;
		 }else if(axis==1){
			 roll = angle;
		 }else if(axis==2){
			 pitch = angle;
		 }
		 this.setLocalRotation(Quaternion.ZERO.fromAngles(yaw, roll,
		 pitch));
		}
		
		
	}
	@Override
	public void use(){
		axis = (axis + 1)%3;
	}

	public int getAxis() {
		return axis;
	}

	public void setAxis(int axis) {
		this.axis = axis;
	}

	@Override
	public String scriptExport() {
		StringBuffer sb = new StringBuffer();
		sb.append(super.scriptExport());
		sb.append(this.getExportVariable()+".setAxis("+getAxis()+");\n");
		return sb.toString();
	}
	
	

}
